package com.sdslzyxyhtgl.modules.menu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdslzyxyhtgl.modules.menu.dao.MenuDao;
import com.sdslzyxyhtgl.modules.menu.entity.MenuEntity;
import com.sdslzyxyhtgl.modules.menu.service.MenuService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service("menuService")
public class MenuServiceImpl extends ServiceImpl<MenuDao, MenuEntity> implements MenuService {

//    @Override
//    public PageUtils queryPage(Map<String, Object> params) {
//        IPage<MenuEntity> page = this.page(
//                new Query<MenuEntity>().getPage(params),
//                new QueryWrapper<MenuEntity>()
//        );
//
//        return new PageUtils(page);
//    }

    @Override
    //返回二级分类的数据
    public List<MenuEntity> listWithTree() {
        //1.查出所有分类
        List<MenuEntity> menuEntities = baseMapper.selectList(null);

        //2.组装成父子的树形结构
        //(1.找到所有的一级分类
        List<MenuEntity> levelmenus = menuEntities.stream().filter(menuEntity ->
                menuEntity.getParentId() == 0).map((menu) -> {
            menu.setChildren(getChildrens(menu, menuEntities)); //对子菜单进行排序
            return menu;
        }).collect(Collectors.toList());   //收集成列表
        return levelmenus;
    }

    private List<MenuEntity> getChildrens(MenuEntity root, List<MenuEntity> all) {
        List<MenuEntity> childrenMenus = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentId().equals(root.getId());
            //同样，子菜单也可能有自己的子菜单，所以需要递归.map的作用便是让元素以另一种形式输出。
        }).collect(Collectors.toList());

        return childrenMenus;
    }

    @Override
    //TODO 二级分类删除
    public void removeids(List<Integer> asList) {
        baseMapper.deleteBatchIds(asList);
    }

}